package com.yiren.manager.ctrl.personnel;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrJobScheduling;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.personnel.YrJobSchedulingForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.personnel.YrJobSchedulingService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("jobScheduling")
@Controller
public class YrJobSchedulingController extends GenericController<YrJobSchedulingForm, YrJobScheduling>{
	
	@Autowired
	private YrJobSchedulingService selfService;
	
	@Autowired
	private YrStaffInfoService yrStaffInfoService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrJobSchedulingForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrJobSchedulingForm> result = new HTTPResult<YrJobSchedulingForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrJobSchedulingForm form = tool.mapper("jobScheduling", YrJobSchedulingForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getJobSchedulingId()||form.getJobSchedulingId().isEmpty()){
			form.setJobSchedulingId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrJobScheduling entity = ObjectUtils.objectToObject(form, YrJobScheduling.class);
		selfService.save(entity);
		
		return result;
	}
	
	
	/**获取所有记录*/
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrJobSchedulingForm> findAll() throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrJobSchedulingForm> result = new HTTPResult<YrJobSchedulingForm>();
		List<YrJobScheduling> entityList = selfService.findAll();
		List<YrJobSchedulingForm> formList = new ArrayList<YrJobSchedulingForm>();
		for(YrJobScheduling entity : entityList){
			YrJobSchedulingForm form = ObjectUtils.objectToObject(entity, YrJobSchedulingForm.class);
			if(null!=form.getUpdateId()){
				YrStaffInfo staffInfo = yrStaffInfoService.findOne(form.getUpdateId());
				if(null!=staffInfo){
					form.setUpdateName(staffInfo.getName());
					form.setDepartmentName(staffInfo.getDepartmentName());
				}
			}
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrJobSchedulingForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrJobSchedulingForm> result = new HTTPResult<YrJobSchedulingForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(String id : ids){
			selfService.delete(id);
		}
		return result;
	}
	
	@Override
	public YrJobSchedulingForm toForm(YrJobScheduling e) throws Exception {
		YrJobSchedulingForm form = ObjectUtils.objectToObject(e, YrJobSchedulingForm.class);
		return form;
	}

	@Override
	public YrJobScheduling toEntity(YrJobSchedulingForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
